package com.example.zsf.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.zsf.R;
import com.example.zsf.adapter.MessageListAdapter;
import com.example.zsf.base.Presenter;
import com.example.zsf.base.Result;
import com.example.zsf.base.ZSFFragment;
import com.example.zsf.base.ZSFGlobal;
import com.example.zsf.model.MessageModel;
import com.example.zsf.presenter.MessagePresenter;
import com.example.zsf.view.NavigationBar;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MessageFragment extends ZSFFragment {


    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private Unbinder unbinder;
    private MessageListAdapter messageListAdapter;

    public MessageFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_message, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initUI();
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        Logger.d("resume");
        if (!ZSFGlobal.checkLogoutStatus(new ZSFGlobal.AccountCallBack() {
            @Override
            public void noneauth() {
                messageListAdapter.setEmptyView(R.layout.view_data_empty);
            }
        })) {
            //登陆的时候请求
            requestMessage();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    private void initUI() {
        navigationBar.setTitleText("消息");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        messageListAdapter = new MessageListAdapter(R.layout.item_message, null);

        recyclerView.setAdapter(messageListAdapter);
        recyclerView.addItemDecoration(new SpaceDecoration());

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestMessage();
            }
        });
    }

    private void requestMessage() {
        disposed(MessagePresenter.getMessage(new Presenter.PresenterListener<Result<ArrayList<MessageModel>>>() {
            @Override
            public void onSuccess(Result<ArrayList<MessageModel>> object) {
                if (object.code.equals("1")) {
                    messageListAdapter.setList(object.data);
                    if (object.data.size() == 0) {
                        messageListAdapter.setEmptyView(R.layout.view_data_empty);
                    }else {
                        if (messageListAdapter.hasEmptyView()) {
                            messageListAdapter.removeEmptyView();
                        }
                    }
                    refreshLayout.finishRefresh();
                } else {
                    ToastUtils.showShort(object.msg);
                    refreshLayout.finishRefresh(false);
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
                refreshLayout.finishRefresh(false);
            }
        }));
    }


    private static class SpaceDecoration extends RecyclerView.ItemDecoration {
        public SpaceDecoration() {
            super();
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(15, 15, 15, 15);
        }
    }
}

